home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / StefView.BackModule / replace.back / StefView.m.back < prev   
Encoding:
Text File  |  1995-06-12  |  5.9 KB  |  249 lines

  1. #import "StefView.h"
  2.  
  3. // this code is copyright Darcy Brockbank, 1993
  4. //
  5. // You may freely reuse and distribute this code in any way shape or
  6. // form, provided that this notice stays intact.
  7. //
  8. // darcy@solutions.ca, samurai@cs.mcgill.ca
  9. // StefView was implemented out of WorldSpaceView and retains some of the
  10. // movement properties...
  11. //
  12. // The code for WorldSpaceView was written by Sam Streeper at NeXT, I think,
  13. // and there were two other contributors, but I can't find their names in the
  14. // source for it.
  15. //
  16. // StefView is the common "Spotlight" screen saver you see on Macs and Windows
  17. // all the time. This is just a quick hack, and can do with vast amounts of
  18. // improvement I'm sure.
  19. //
  20. // If you do improve this thing, send me a copy!
  21. //
  22. // - darcy
  23.  
  24. @implementation StefView
  25.  
  26.  
  27. #define X_PERIOD 15000.0
  28. #define Y_PERIOD 12000.0
  29. #define DEFAULT_FPS 20
  30. #define PI 3.1415926535
  31. #define MAX_IMAGE_WIDTH 256
  32. #define MAX_IMAGE_HEIGHT 256
  33. #define MAX_X_SPEED (26)
  34. #define MAX_Y_SPEED (26)
  35. #define BUFFER_WIDTH (MAX_IMAGE_WIDTH + MAX_X_SPEED + 1)
  36. #define BUFFER_HEIGHT (MAX_IMAGE_HEIGHT + MAX_Y_SPEED + 1)
  37. #define PRINT(a) printf(a)
  38.  
  39. #define MINX 0.0
  40. #define MINY 50.0
  41.  
  42. - takeSpot:(float)x:(float)y;
  43. {
  44.     NXRect r = {{0.0,0.0},{0.0,0.0}};
  45.     NXPoint p = {0.0,0.0};
  46.     [spot lockFocus];
  47.     [spot getSize:&r.size];
  48.     r.origin.x = x;
  49.     r.origin.y = y;
  50.     [clearSpot composite:NX_COPY toPoint:&p];
  51.     [sbuffer composite:NX_DOVER fromRect:&r toPoint:&p];
  52.     [spot unlockFocus];
  53.     return self;
  54. }
  55.  
  56. - takeSnapshot;
  57. {
  58.     id snapShot;
  59.     NXSize screen;
  60.     static NXRect imageRect = {{0.0,0.0},{0.0,0.0}};
  61. //    NXColor transBlack = NXChangeAlphaComponent(NX_COLORBLACK,0.33);
  62.     [NXApp getScreenSize:&screen];
  63.     imageRect.size = screen;
  64.     snapShot =[[Window alloc] initContent:&imageRect
  65.         style:NX_PLAINSTYLE
  66.         backing:NX_NONRETAINED
  67.         buttonMask:0
  68.         defer:NO];
  69.     PSsetwindowlevel(NX_MAINMENULEVEL+1,[snapShot windowNum]);
  70.     PSsetautofill(NO,[snapShot windowNum]);
  71.     [snapShot orderFront:self];
  72.     NXPing();
  73.     if (!sbuffer) {
  74.         sbuffer = [[NXImage alloc] initSize:&screen];
  75.     }
  76.     [sbuffer lockFocus];
  77. //    NXSetColor(transBlack);
  78. //    NXRectFill(&imageRect);
  79.     PScomposite(0.0,0.0,screen.width,screen.height,[snapShot gState],
  80.             0.0,0.0,NX_COPY);
  81.     [sbuffer unlockFocus];
  82.     snapShot = [snapShot free];
  83.     return self;
  84. }
  85.  
  86. - oneStep
  87. {
  88.     NXRect black = {0,0,0,0};
  89.     NXRect ballRect;
  90.     BRECT new;
  91.  
  92.     then = now;
  93.     now = currentTimeInMs();
  94.  
  95.     xpos = ((1 + sin(((float)now) / X_PERIOD * 2. * PI))/2.0) 
  96.         * maxCoord.x + MINX;
  97.     ypos = ((1 + sin(((float)now) / Y_PERIOD * 2. * PI))/2.0) 
  98.         * maxCoord.y + MINY;
  99.     
  100.     if (xpos < (old.l - MAX_X_SPEED)) xpos = old.l - MAX_X_SPEED;
  101.     else if (xpos > (old.l + MAX_X_SPEED)) xpos = old.l + MAX_X_SPEED;
  102.  
  103.     if (ypos < (old.b - MAX_Y_SPEED)) ypos = old.b - MAX_Y_SPEED;
  104.     else if (ypos > (old.b + MAX_Y_SPEED)) ypos = old.b + MAX_Y_SPEED;
  105.  
  106.     new.l = floor(xpos);
  107.     new.b = floor(ypos);
  108.     new.r = new.l + imageSize.width;
  109.     new.t = new.b + imageSize.height;
  110.  
  111.     [self takeSpot:new.l :new.b];
  112.     ballRect.origin.x = 0;
  113.     ballRect.origin.y = 0;
  114.     ballRect.size.width = imageSize.width;
  115.     ballRect.size.height = imageSize.height;
  116.     
  117.     redrawTo.x = MIN(new.l, old.l);
  118.     redrawTo.y = MIN(new.b, old.b);
  119.  
  120.     redraw.origin.x = 0;
  121.     redraw.origin.y = 0;
  122.     redraw.size.width = (MAX(new.r, old.r)) - redrawTo.x + 1;
  123.     redraw.size.height = (MAX(new.t, old.t)) - redrawTo.y + 1;
  124.     
  125.     black.size= redraw.size;
  126.  
  127.     [buffer lockFocus];
  128.     PSsetgray(0);
  129.     NXRectFill(&black);
  130.     
  131.     ballTo.x = new.l - redrawTo.x;
  132.     ballTo.y = new.b - redrawTo.y;
  133.  
  134.     [spot composite:NX_SOVER fromRect:&ballRect toPoint:&ballTo];
  135.     [buffer unlockFocus];
  136.     [buffer composite:NX_COPY fromRect:&redraw toPoint:&redrawTo];
  137.     old = new;
  138.     return self;
  139. }
  140.  
  141.  
  142. - initFrame:(const NXRect *)frameRect
  143. {
  144.     const char *animSpeed;
  145.  
  146.     NXRect black = {0, 0, BUFFER_WIDTH, BUFFER_HEIGHT };
  147.     [super initFrame:frameRect];
  148.     [self allocateGState];        // For faster lock/unlockFocus
  149.     [self setClipping:NO];        // even faster...
  150.     //in this case, I only need one buffer for several Views
  151.     if (!(buffer = [NXImage findImageNamed:"worldBuffer"]))
  152.     {
  153.         buffer = [[NXImage alloc] initSize:&black.size];
  154.         [((NXImage *)buffer) setName:"worldBuffer"];
  155.     }
  156.     if ([buffer lockFocus])
  157.     {
  158.         PSsetgray(0);
  159.         NXRectFill(&black);
  160.         [buffer unlockFocus];
  161.     }
  162.     animSpeed = NXGetDefaultValue([NXApp appName], "animSpeed");
  163.     if (animSpeed == NULL) framesPerSecond = DEFAULT_FPS;
  164.     else framesPerSecond = atoi(animSpeed);
  165.     nextRotationTime = 0;
  166.     [self newViewSize];
  167.     return self;
  168. }
  169.  
  170. - sizeTo:(NXCoord)width :(NXCoord)height
  171. {
  172.     [super sizeTo:width :height];
  173.     [self newViewSize];
  174.     return self;
  175. }
  176.  
  177. - drawSelf:(const NXRect *)rects :(int)count
  178. {
  179. //    [sbuffer composite:NX_COPY fromRect:rects toPoint:&(rects->origin)];
  180.     return self;
  181. }
  182.     
  183.     
  184. - newViewSize
  185. {
  186.     //this is called every time View size changes
  187.     NXRect black = {0, 0, BUFFER_WIDTH, BUFFER_HEIGHT };
  188.  
  189.     then = now = currentTimeInMs();
  190.  
  191.     if (oldSize.width == bounds.size.width &&
  192.             oldSize.height == bounds.size.height)
  193.         return self;
  194.     else
  195.     {
  196.         oldSize.width = bounds.size.width;
  197.         oldSize.height = bounds.size.height;
  198.     }
  199.     
  200.     maxCoord.x = bounds.size.width - imageSize.width - MINX;
  201.     maxCoord.y = bounds.size.height - imageSize.height - MINY;
  202.     if (maxCoord.x < 0) maxCoord.x = 0;
  203.     if (maxCoord.y < 0) maxCoord.y = 0;
  204.  
  205.  
  206.     old.l = old.r = maxCoord.x/2 + MINX;
  207.     old.b = old.t = maxCoord.y/2 + MINY;
  208.     ballTo.x = ballTo.y = 0;
  209.  
  210.     if ([buffer lockFocus])
  211.     {
  212.         PSsetgray(0);
  213.         NXRectFill(&black);
  214.         [buffer unlockFocus];
  215.     }
  216.  
  217.     return self;
  218. }
  219.  
  220. - (const char *)windowTitle
  221. {
  222.     return "StefView";
  223. }
  224.  
  225.  
  226. - inspector:sender
  227. {
  228.     char buf[MAXPATHLEN];
  229.  
  230.     if (!sharedInspectorPanel){
  231.         sprintf(buf,"%s/StefView.nib",[sender moduleDirectory:"Stef"]);
  232.         [NXApp loadNibFile:buf owner:self withNames:NO];
  233.         if (!spot) {
  234.             sprintf(buf,"%s/Spot.tiff",[sender moduleDirectory:"Stef"]);
  235.             spot = [[NXImage alloc] initFromFile:buf];
  236.             [spot lockFocus];
  237.             [spot unlockFocus];
  238.             clearSpot = [[NXImage alloc] initFromFile:buf];
  239.             [clearSpot lockFocus];
  240.             [clearSpot unlockFocus];
  241.             [spot getSize:&imageSize];
  242.         }
  243.     }
  244.     [self takeSnapshot];
  245.     return sharedInspectorPanel;
  246. }
  247.  
  248. @end
  249.